// pages/my/my.js
var userModel=require('./model/UserModel.js')
import SystemInfoUtil from '../../utils/SystemInfoUtil.js'
var order_url = "../view-order/view-oreder?type="
Page({

  /**
   * 页面的初始数据
   */
  data: {
    headerAdv: '',
    userName: '',
    isIOS: false,
    optlist: [
      { name: '我的粉币', url: '../mywallet/wallet', icon: '../../images/icons/icon_fenbi.png' },
      { name: '已购课程', url: '../myorder/order', icon: '../../images/icons/icon_order.png' },
      { name: '我的收藏', url: '../mycollection/collection', icon: '../../images/icons/icon_my_collect.png' },
      { name: '分销专区', url: '../distribution/distribution', icon: '../../images/icons/icon_fx.png' },
      { name: '推荐有奖', url: '../shareapp/share', icon: '../../images/icons/icon_recommend.png' },
      { name: '意见反馈', url: '../feedback/feedback', icon: '../../images/icons/icon_feedback.png' },
      { name: '关于我们', url: '../aboutapp/about', icon: '../../images/icons/icon_about_us.png' },
      { name: '客服热线', url: 'callPhone', icon: '../../images/icons/icon_phone1.png' }]
  },
  editorUserInfo:function(event){
    wx.navigateTo({
      url: '../editor-userinfo/editor',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    var _isIOS = SystemInfoUtil.platform == SystemInfoUtil.IOS
    this.setData({
      isIOS: _isIOS
    })
    if (_isIOS){
      var name = 'optlist[' + 1 + '].name';
      this.setData({
        [name]: '已订阅课程'
      })
    }
    var userInfo = getApp().globalData.userInfo
    if (userInfo==null||userInfo==undefined){
      userModel.getUserInfo(function (data) {
        getApp().globalData.userInfo = data
        if (data.userIcon == "https://web.fenbibox.com/chalkImages"){
          data.userIcon ='/images/userhead.png';
        }
        that.setData({
          headerAdv: data.userIcon,
          userName: data.userName
        })
      })
    }else{
      if (userInfo.userIcon == "https://web.fenbibox.com/chalkImages") {
        userInfo.userIcon = '/images/userhead.png';
      }
      this.setData({
        headerAdv: userInfo.userIcon,
        userName: userInfo.userName
      })
    }
  },
  
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 查看所以状态订单
   */
  findAllStatusOrders: function (event) {
    var _url = order_url + "0"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 未付款订单
   */
  findWaitPayOrder: function (event) {
    var _url = order_url + "1"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 未发货订单
   */
  findWaitTransOrder: function (event) {
    var _url = order_url + "2"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 等待收货订单
   */
  findWaitReceviceOrder: function (event) {
    var _url = order_url + "3"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 等待评价订单
   */
  findWaitPinjiaOrder: function (event) {
    var _url = order_url + "4"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 售后
   */
  findWaitServiceOrder: function (event) {
    var _url = order_url + "5"
    wx.navigateTo({
      url: _url,
    })
  },
  /**
   * 功能点击
   */
  optListItemClick: function (event) {
    var pageurl = event.currentTarget.dataset.url
    if (pageurl != null && pageurl.length > 0) {
      if ("callPhone"==pageurl){
        wx.makePhoneCall({
          phoneNumber: '400-616-8880',
        })
        return
      }
      wx.navigateTo({
        url: pageurl,
      })
    } else {
      wx.showToast({
        title: '功能暂未开放!',
        icon: 'none'
      })
    }
  },
  exitAccount:function(event){
    wx.showModal({
      title: '提示',
      content: '点击确定将退出粉笔乐学应用',
      showCancel: true,
      success: function (res) {
        if (res.cancel) {
          //点击取消,默认隐藏弹框
        } else {
          //点击确定
          userModel.accountExit(function(data){
              //清理数据(全局用户数据)
              getApp().globalData.userInfo=null
              wx.removeStorageSync("userToken")
              wx.removeStorageSync("phone")
              wx.showToast({
                title: '退出成功!',
              })
              wx.reLaunch({
                url: '../login/login',
                success: function (res) { },
                fail: function (res) { },
                complete: function (res) { },
              })
          })
        }
      }
    })
  }
})